﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;
using System.Data;
using System.DirectoryServices;

//////////////////////////////////////////////////////////////////////////
//AdviseUPAdvisee.master.cs                                             //
//Last Edited: 23 Mar 2012                                              //
//                                                                      //
//Contains common functionality for advisees.                           //
//This includes ensuring users are logged in, maintaining session       //
//  variables, initializing advisee information, and logging out.       //
//////////////////////////////////////////////////////////////////////////
public partial class AdviseUP : System.Web.UI.MasterPage
{
    //////////////////
    //Page Functions//
    //////////////////

    /// <summary>
    /// Page_Load()
    /// 
    /// Initializes the master page and common elements across
    ///     all advisee pages.
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        //Decrypt the login cookie to obtain user name
        String encryptedTicket = Request.Cookies.Get(FormsAuthentication.FormsCookieName).Value;
        FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(encryptedTicket);
        //Query the Active Directory Server to obtain ID number
        DirectoryEntry entry = new DirectoryEntry("LDAP://campus.up.edu/" + SharedFunctions.GetDistinguishedName(authTicket.Name).Substring(7));
        String IDnumber = entry.Properties["description"].Value.ToString();
        entry.Close();
        //Redirect if an advisor tries to access advisee pages
        SharedFunctions.UserLevel ul = SharedFunctions.getUserLevel(authTicket.Name);
        if (!(ul.Equals(SharedFunctions.UserLevel.ADVISEE))) Response.Redirect("~/Advisor/Default.aspx");

        //Initialize Session Variables and web controls
        //Session - AdviseeID
        Session["Advisee_adviseeID"] = IDnumber;
        //Session - Advisee Name and controls
        Dictionary<String, Object> results = StoredProcedures.getAdviseeName(Session["Advisee_adviseeID"] as String);
        AdviseeNameTop.Text = results["lastName"] + ", " + results["firstName"];
        AdviseeName.Text = results["firstName"] + " " + results["lastName"];
        Session["Advisee_firstName"] = results["firstName"];
        Session["Advisee_lastName"] = results["lastName"];
        //Advisor Name and controls
        results = StoredProcedures.getAdvisor(Session["Advisee_adviseeID"] as String);
        //AdvisorName.Text = results["firstName"] + " " + results["lastName"];
        //Session - Start Term and controls
        results = StoredProcedures.getAdviseeStartTerm(Session["Advisee_adviseeID"] as String);
        Session["Advisee_startTerm"] = results["startTerm"];
        StartDate.Text = SharedFunctions.convertFromTermCode("" + results["startTerm"]);
    } //end Page_Load()


    /// <summary>
    /// 
    /// Allows user to redirect to the default page by clicking on the logo
    /// in the upper right corner
    /// 
    /// Author: Kekai Ariola
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void logo_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Advisee/Default.aspx");
    }

    /// <summary>
    /// signOut_Click()
    /// 
    /// Occurs when a user clicks the Sign Out button on a page.
    /// Ends the session and performs necessary actions.
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void signOut_Click(object sender, EventArgs e)
    {
        //Purge advisee's transcript from internal DB
        StoredProcedures.purgeAdviseeTranscript("" + Session["Advisee_adviseeID"], "" + Session["Advisee_adviseeID"]);
        //Built-in method to clear session
        FormsAuthentication.SignOut();
        //Redirect to login page
        Response.Redirect("~/Default.aspx?logout=success");
    } //end signOut_Click()
}

